package org.example.examsystem.service;


import org.example.examsystem.entity.Exam;
import org.example.examsystem.entity.Paper;
import org.example.examsystem.entity.StudentExam;
import org.example.examsystem.mapper.ExamMapper;
import org.example.examsystem.mapper.PaperMapper;
import org.example.examsystem.mapper.StudentExamMapper;
import org.example.examsystem.mapper.StudentSubjectMapper;
import org.example.examsystem.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private StudentExamMapper studentExamMapper;
    @Autowired
    private StudentSubjectMapper studentSubjectMapper;



    @Override
    public Page<Exam> getExams(Pageable pageable) {
        int page = pageable.getPageNumber() * pageable.getPageSize();
        int size = pageable.getPageSize();
        List<Exam> exams = examMapper.getExams(page, size);
        int total = examMapper.getTotal();
        return new PageImpl<>(exams, pageable, total);
    }

    @Override
    public void addExam(Exam exam) {
        examMapper.addExam(exam);
    }

    @Override
    public void updateExam(Exam exam) {
        examMapper.updateExam(exam);
    }

    @Override
    public void deleteExam(int id) {
        // 先删除 studentsubject 表中关联的数据
        studentSubjectMapper.deleteByExamId(id);
        // 再删除 studentexam 表中关联的数据
        studentExamMapper.deleteByExamId(id);
        // 然后删除 paper 表中关联的数据
        paperMapper.deleteByExamId(id);
        // 最后删除 exam 表中的数据
        examMapper.deleteExam(id);
    }

    @Override
    public void batchDeleteExams(List<Integer> ids) {
        // 先批量删除 studentsubject 表中关联的数据
        studentSubjectMapper.batchDeleteByExamIds(ids);
        // 再批量删除 studentexam 表中关联的数据
        studentExamMapper.batchDeleteByExamIds(ids);
        // 然后批量删除 paper 表中关联的数据
        paperMapper.batchDeleteByExamIds(ids);
        // 最后批量删除 exam 表中的数据
        examMapper.batchDeleteExams(ids);
    }

    @Override
    public Exam saveExam(Exam exam) {
        examMapper.insertExam(exam);
        return examMapper.getExamById(exam.getEid());
    }

    @Override
    public Exam getExamById(int eid) {
        return examMapper.getExamById(eid);
    }
    @Override
    public Exam getExamDetailsById(Integer examId) {
        return examMapper.getExamDetailsById(examId);
    }

    @Override
    public StudentExam submitExam(Integer examId, Integer userId, List<String> answers) {
        // 查询试卷详细信息
        Exam exam = examMapper.getExamDetailsById(examId);
        List<Paper> papers = examMapper.getPapersByExamId(examId);

        // 计算得分
        int score = 0;
        Integer singleChoiceScore = exam.getSingleChoiceScore();
        if (singleChoiceScore == null) {
            singleChoiceScore = 0; // 若为null，设为默认值0
        }
        for (int i = 0; i < papers.size(); i++) {
            Paper paper = papers.get(i);
            if (answers.get(i).equals(paper.getSkey())) {
                score += singleChoiceScore;
            }
        }

        // 保存考试结果到studentexam表
        StudentExam studentExam = new StudentExam();
        studentExam.setUserid(userId);
        // 通过userid获取classid
        int classId = examMapper.getClassIdByUserId(userId);
        studentExam.setClassid(classId);
        studentExam.setEid(examId);
        studentExam.setPname(exam.getPname());
        studentExam.setZscore(exam.getTotalScore());
        studentExam.setScore(score);
        studentExam.setTjtime(new Date());
        examMapper.insertStudentExam(studentExam);

        // 获取生成的seid
        Integer seid = examMapper.getLastInsertId();

        // 将每道题的学生答案保存到studentsubject表
        for (int i = 0; i < papers.size(); i++) {
            Paper paper = papers.get(i);
            examMapper.insertStudentSubject(seid, userId, examId, paper.getSid(), answers.get(i));
        }

        return studentExam;
    }

    @Override
    public Exam getAnsweredExamDetails(Integer examId) {
        return examMapper.getAnsweredExamDetails(examId);
    }


}
